﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;
using static 货物监控.XY_task;

namespace 货物监控
{
    internal class SQL_Func
    {
        // 读码去查询是否存在当前任务
        public int  getSU(string SU)
        {

            string sql = @"select count(1) from XY_taskdetail where [storage_unit_number]='" + SU + "'";

            object count = DAL.DapperHelper<XY_taskdetail>.ExecuteScalar(sql);

            return Convert.ToInt16(count);
        }
        // 查询SU号，通过返回的DN号和Item号查询数据库。
        public XY_taskdetail SelectStorage_unit_number(string storage_unit_number)
        {

            string sql = @"SELECT top 1 *
            FROM [dbo].[XY_taskdetail]   
            where [storage_unit_number]='" + storage_unit_number + "'";

            XY_taskdetail t = DAL.DapperHelper<XY_taskdetail>.QuerySingleOrDefault(sql);
            return t;
        }


        // 在ST10完成后，更新数据库
        public bool Update10(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.扫码) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);

            return count > 0 ? true : false;
        }

        // 在ST20完成后，更新数据库
        public bool Update20(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.写入程序) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);

            return count > 0 ? true : false;
        }
        // 在ST30完成后，更新数据库
        public bool Update30(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.缠膜) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }

        // 在ST40完成后，更新数据库
        public bool Update40(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.贴标) + "',MState='" + Convert.ToInt16(state) + "',F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

        int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }
        // 在ST42完成后，更新数据库
        public bool Update42(string SU号, string HU, WorkState state, string PrintTask)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  HU = '" + HU + "',MState='" + Convert.ToInt16(state) + "',PrintTask='" + PrintTask + "', F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }
        // 在ST50完成后，更新数据库
        public bool Update50(string SU号, WorkState state)
        {

            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.定位) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }
        // 在ST60完成后，更新数据库
        public bool Update60(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.提升机) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";


            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }


        // 在ST70完成后，更新数据库
        public bool Update70(string SU号, WorkState state)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.打包运走) + "',MState='" + Convert.ToInt16(state) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "'  and F_DeleteMark=0";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }
        // 在ST80完成后，更新数据库
        public bool Update80(string SU号)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.存放完成) + "' ,F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "' and MSite=70 and F_DeleteMark=0";


            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }
        // 在ST80完成后，更新数据库
        public bool Update90(string SU号)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  [MSite] = '" + Convert.ToInt16(WorkSite.发货) + "' ,F_LastModifyTime=getdate(),IsTo='已扫码' WHERE [storage_unit_number]='" + SU号 + "' and F_DeleteMark=0";


            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }


        /// <summary>
        /// 订单中最后一托货
        /// </summary>
        /// <param name="SU"></param>
        /// <returns></returns>
        public static PushLine getSAP70(string SU)
        {

            string sql = @"select t.headerId,d.orderID,Min(MSite)MSite from  XY_task t left join XY_taskdetail d on t.F_Id=d.Tcid
where d.dropdown_station_storage_bin in(
select dropdown_station_storage_bin from XY_taskdetail where storage_unit_number='" + SU + "') group by t.headerId,d.orderID";

            PushLine Sap = DAL.DapperHelper<PushLine>.QueryFirstOrDefault(sql);

            return Sap;
        }



        public static bool UpdateSAP70(string SU)
        {

            string sql = @"update XY_taskdetail set IsTo=getdate() where dropdown_station_storage_bin in(
select dropdown_station_storage_bin from XY_taskdetail where storage_unit_number='" + SU + "') ";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);

            return (count > 0);
        }
        // 更新HU号
        public bool UpdateHU(string SU号, string HU)
        {
            string sql = @"UPDATE [dbo].[XY_taskdetail] SET  HU= '" + HU + "',F_LastModifyTime=getdate() WHERE [storage_unit_number]='" + SU号 + "'";

            int count = DAL.DapperHelper<XY_taskdetail>.Execute(sql);
            return count > 0 ? true : false;
        }

        // 查询零件号，获取参数
        public XY_part SelectPNumber(string PNumber)
        {
            string sql = "SELECT * FROM [XY_part] where PNumber = '" + PNumber + "'";
            XY_part p = DapperHelper<XY_part>.QueryFirstOrDefault(sql);
            return p;
        }




    }
}
